package com.atguigu.srb.core.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * 访问控制、解决跨域
 * @author xckj_zhangyuliang
 *
 */
@Configuration
public class AccessControlAllowOriginConfig implements WebMvcConfigurer {
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")
		.allowCredentials(true)
		.allowedOrigins("*")
		.allowedHeaders("*")
		.allowedMethods(new String[]{"GET","POST","PUT","DELETE","OPTIONS"})
//		.exposedHeaders("*")
		;
	}

	@Override
	public void addFormatters(FormatterRegistry registry) {
//		registry.addConverter(new DateConverter());
	}

	public static class DateConverter implements Converter<String, Date>  {
		@Override
		public Date convert(String str) {
			if (StringUtils.isBlank(str)) {
				return null;
			}

			return null;
		}
	}
	public static class LocalDateConverter implements Converter<String, LocalDate>  {
		@Override
		public LocalDate convert(String str) {
			if (StringUtils.isBlank(str)) {
				return null;
			}

			return null;
		}
	}
	public static class LocalDateTimeConverter implements Converter<String, LocalDateTime>  {
		@Override
		public LocalDateTime convert(String str) {
			if (StringUtils.isBlank(str)) {
				return null;
			}

			return null;
		}
	}
}
